package com.tac.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tac.common.R;
import com.tac.pojo.Kind;
import com.tac.pojo.Product;
import com.tac.service.KindService;
import com.tac.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/kind")
@CrossOrigin
public class KindController {
    @Autowired
    private KindService kindService;
    @Autowired
    private ProductService productService;

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/select")
    public R select() {
        List<Kind> list = kindService.list();
        return R.success(list,"success");
    }

    /**
     * 根据id删除分类
     * @param kid
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(Integer kid){
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getKind,kid);
        List<Product> list = productService.list(queryWrapper);
        if(list.size()==0){
            kindService.removeById(kid);
            return R.success(null,"删除成功");
        }else {
            return R.error("该分类下存在在售商品无法删除");
        }
    }
}
